﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasAmphurDAL : IMasAmphurDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasAmphurDAL()
        {

        }
        public IList<MasAmphurInfo> ListAmphurs(MasAmphurInfo ampInfo)
        {
            string strSQL;
            Nullable<int> proSeq = ampInfo.Provinces == null ? 0 : ampInfo.Provinces.Sequence;
            IList<MasAmphurInfo> infoList = new List<MasAmphurInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", ampInfo.Sequence);
                dbManager.AddParameters(2, "@Province", proSeq);

                strSQL = "SELECT AMP_SEQ, AMP_NAME, AMP_NAME_ENG, PROV_SEQ, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_AMPHURS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (ampInfo.Sequence > 0 ? " AND AMP_SEQ = @Seq" : "")
                    + (proSeq > 0 ? " AND PROV_SEQ = @Province" : "")
                    + " ORDER BY AMP_NAME";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasProvinceDAL proDAL = new MasProvinceDAL();
                    MasProvinceInfo proInfo = new MasProvinceInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<MasProvinceInfo> proList = proDAL.ListMasProvinces(new MasProvinceInfo(rdr.IsDBNull(3) ? 0 : rdr.GetInt32(3)));
                        if(proList.Count > 0) proInfo = proList[0];
                    }

                    MasAmphurInfo Info = new MasAmphurInfo( Convert.ToInt32(rdr.GetDecimal(0))
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , proInfo
                            , rdr.IsDBNull(4) ? (int?)null : rdr.GetInt32(4)
                            , rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5));
                    infoList.Add(Info);

                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }
        public void Insert(MasAmphurInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Name", info.Name);
                dbManager.AddParameters(1, "@NameEng", info.NameEng);
                dbManager.AddParameters(2, "@Province", info.Provinces.Sequence);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_AMPHURS(AMP_NAME, AMP_NAME_ENG, PROV_SEQ"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @NameEng, @Province, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasAmphurInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Seq", info.Sequence);
                dbManager.AddParameters(1, "@Name", info.Name);
                dbManager.AddParameters(2, "@NameEng", info.NameEng);
                dbManager.AddParameters(3, "@Province", info.Provinces.Sequence);
                dbManager.AddParameters(4, "@UpdatedBy", info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_AMPHURS"
                           + " SET AMP_NAME = @Name"
                                + ", AMP_NAME_ENG = @NameEng"
                                + ", PROV_SEQ = @Province"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE AMP_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(MasAmphurInfo info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", info.Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE MAS_AMPHURS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE AMP_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
